package com.easytouch.vm.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.easytouch.core.Configure;
import com.easytouch.core.utils.SystemTools;
import com.easytouch.vm.R;
import com.easytouch.vm.service.DriverService;
import com.easytouch.vm.service.TYPayLongService;
import com.easytouch.vm.service.TYService;

public class SplashActivity extends AppCompatActivity {

    private TextView mMessageTextView;


    @SuppressLint("StaticFieldLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        mMessageTextView = (TextView) findViewById(R.id.tv_message);


        new AsyncTask<Void, Integer, Void>() {
            @Override
            protected void onPreExecute() {
                mMessageTextView.setText(R.string.main_welcome);
            }

            @Override
            protected Void doInBackground(Void... params) {
                SystemClock.sleep(500);
                //设置隐藏system bar
                publishProgress(R.string.main_setting);
                //SystemTools.hideSystemBar();
                SystemClock.sleep(500);
                //读取商品数据
                publishProgress(R.string.main_read_goods);
                Configure.loadGoodsXML();
                SystemClock.sleep(500);
                //读取基本配置
                publishProgress(R.string.main_read_config);
                if (Configure.loadConfig() != Configure.LOAD_DATA_SUCCESS) {
                    SystemClock.sleep(500);
                    //使用默认配置
                    publishProgress(R.string.main_read_config_error);
                    Configure.createDefaultConfig();
                }
                SystemTools.setRebootTime(); //设置重启时间
                SystemClock.sleep(500);
                //开启驱动服务
                publishProgress(R.string.main_start_service);
                Intent service = new Intent(SplashActivity.this, DriverService.class);
                startService(service);
                publishProgress(R.string.main_start_service);

//                Intent service2 = new Intent(SplashActivity.this, PayLongService.class);
//                startService(service2);

//                Intent service2 = new Intent(SplashActivity.this, AliDemoService.class);
//                startService(service2);

                Intent service3 = new Intent(SplashActivity.this, TYService.class);
                startService(service3);

                Intent service4 = new Intent(SplashActivity.this, TYPayLongService.class);
                startService(service4);
                return null;
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                int res = values[0].intValue();
                mMessageTextView.setText(res);
                super.onProgressUpdate(values);
            }

            @Override
            protected void onPostExecute(Void result) {
                //完成
                super.onPostExecute(result);
            }

        }.execute();
    }
}
